package cn.jiedh.app.meiweidk.web.webhandle;

import android.content.Context;
import android.support.v4.app.Fragment;

import com.google.gson.GsonBuilder;

import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.web.WebActivity;
import cn.jiedh.app.meiweidk.web.webkit.WJBridgeHandler;
import cn.jiedh.app.meiweidk.web.webkit.WJCallbacks;

/**
 * @name：
 * @author： liuyun
 * @phone： 15723310658
 * @createTime： 2017/12/14
 * @modifyTime： 2017/12/14
 * @explain：说明
 */


public class OpenUrlHandle implements WJBridgeHandler {
    public static final String HANDLE_NAME="openUrl";
    private Context mCox;
    private Fragment mFragment;

    public OpenUrlHandle(Context mCox) {
        this.mCox = mCox;
    }

    public OpenUrlHandle(Fragment mFragment) {
        this.mFragment = mFragment;
    }

    @Override
    public void handler(String data, WJCallbacks callbacks) {
        if(L.isDebug) {
            System.out.println("OpenUrlHandle-----------------------------------------------------------------------------");
            System.out.println("data-->" + data);
        }
        OpenUrlHandleBean bean=null;
        try {
            bean= new GsonBuilder().create().fromJson(data,OpenUrlHandleBean.class);
            if(!bean.isShareEnable()) {
                WebActivity.launch(bean.getUrl(), bean.getTitle(), mCox != null ? mCox : mFragment.getActivity());
            }else{
                WebActivity.launch(mCox != null ? mCox : mFragment.getActivity(),bean.getUrl(),bean.getTitle(),bean.getShareBean());
            }
        }catch (Exception e){
            if(L.isDebug){
                L.e("解析异常");
                e.printStackTrace();
            }
            return;
        }
    }
}
